package com.stepforward.me;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class TripDetail extends  Activity {
	ListView list;
	String[] routeInfo = new String[] { 	
	"5TH AVE & MOREWOOD AVE to ELLSWORTH AVE & S AIKEN AVE - Itinerary 1",
	"Route : (71D) HAMILTON DOWNTOWN VIA OAKLAND Inbound\nGet On : Board at 5TH AVE & MOREWOOD\nOn Time: 8:07 AM\nGet Off : Get Off at 5TH AVE & BIGELOW (PITT)\nOff Time: 8:11 AM",
	"Route : (75) ELLSWORTH BAKERY SQUARE VIA OAKLAND Outbound\nGet On : Board at 5TH AVE & BIGELOW\nOn Time: 8:18 AM\nGet Off : Get off at ELLSWORTH AVE & AIKEN\nOff Time: 8:25 AM",
	"Total Trip Time is 18 minutes" 

	};
	ArrayAdapter<String> listAdapter;
	Route r = new Route();
	Button addAlarmBtn ;
	Button addFavBtn ;
	String departure, arrival;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.plandetail);  
		list = (ListView) findViewById(R.id.list);
		listAdapter = new ArrayAdapter<String>(this, R.layout.list_item,
				routeInfo);
		list.setAdapter(listAdapter);
		addAlarmBtn = (Button) findViewById(R.id.addalarmabtn);
		addFavBtn=(Button)findViewById(R.id.addtofav);
		Bundle extras = getIntent().getExtras();
		departure = extras.getString("departure");
		arrival = extras.getString("arrival");
		String month = extras.getString("month");
		String day = extras.getString("day");
		String year = extras.getString("year");
		String hour = extras.getString("hour");
		String minute = extras.getString("minute");
		String pm = extras.getString("ampm");

	
		try {
	//Boolean isRouteAvailable=	 r.getRoute("Ellsworth ave at aiken ave", "5th ave at morewood",
			// "4", "26", "2011", "19", "32", "pm");
		Boolean isRouteAvailable =	r.getRoute(departure, arrival, month, day, year, hour, minute, pm);
		if (isRouteAvailable == false){
			Intent intent = new Intent();
			 intent.setAction("clarifyAddress");
			 intent.putExtra("departure",departure );
			 intent.putExtra("arrival",arrival);
			 intent.putExtra("month", month);
			 intent.putExtra("day", day);
			 intent.putExtra("year", year);
			 intent.putExtra("hour", day);
			 intent.putExtra("minute", year);
			 intent.putExtra("ampm", pm);
			 
			 intent.putExtra("type", r.getSuggestion().getType());
			 intent.putExtra("sugesstion",r.getSuggestion().getSuggestions());
			
			// startActivity(intent);
			 
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		 String[] itineraties = new String[]{"Itineraty 1" , "Itineraty 2"};
		 if(r!=null){
		itineraties = new String[r.getItineraries().size()];

		for (int i = 0; i < itineraties.length; i++) {
			itineraties[i] = "Itineraty " + (i + 1);
		}
		 }
			
		// String[] itineraties = new String[]{"a","b"};
		Spinner spinner = (Spinner) findViewById(R.id.spinner);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, itineraties);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

	
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				Toast.makeText(
						parent.getContext(),
						"The planet is "
								+ parent.getItemAtPosition(pos).toString(),
						Toast.LENGTH_LONG).show();
				Itinerary itinerary = r.getItineraries().get(pos);
				routeInfo = new String[itinerary.getTransfers().size() + 2];
		
				routeInfo[0] = itinerary.getHeader();
				for (int i = 1; i <= itinerary.getTransfers().size(); i++) {
					routeInfo[i] = itinerary.getTransfers().get(i - 1)
							.toString();
				}
				routeInfo[itinerary.getTransfers().size() + 1] = itinerary
						.getTotalTime();

				listAdapter = new ArrayAdapter<String>(TripDetail.this,
						R.layout.list_item, routeInfo);
				list.setAdapter(listAdapter);


			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		addAlarmBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String busInfo = routeInfo[1];
				Intent intent = new Intent();
				intent.setAction("AddAlarm");
				intent.putExtra("departure", departure);
				intent.putExtra("arrival", arrival);
				intent.putExtra("busInfo", busInfo);
				startActivity(intent);
			}
			
		});
		addFavBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Intent intent = new Intent();
				intent.setAction("AddFavorite");
				intent.putExtra("departure", departure);
				intent.putExtra("arrival", arrival);
				intent.putExtra("flag", "true");
				startActivity(intent);
			}
			
		});
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				if (position>0 && position!= parent.getCount()-1){
					String s = (String)parent.getItemAtPosition(position);
					String[] splits = s.split("\n");
					String departure = null;
					String arrival = null;
				
						departure = splits[1].substring(17);
				
					
					arrival = splits[3].substring(22);
					Intent intent = new Intent();
					 intent.setAction("MapViewer");
					 intent.putExtra("departure",departure );
					 intent.putExtra("arrival",arrival );
					 startActivity(intent);
					//Toast.makeText(TripDetail.this,splits[1].substring(17),Toast.LENGTH_SHORT).show();
				}
			}
		
		});



	}
}

class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;

	private Integer[] mImageIds = { R.drawable.ic_menu_tag, R.drawable.ic_menu_clock,
			R.drawable.ic_menu_light, R.drawable.ic_menu_love};

	public ImageAdapter(Context c) {
		mContext = c;

		TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
		mGalleryItemBackground = a.getResourceId(
				R.styleable.HelloGallery_android_galleryItemBackground, 0);
		a.recycle();
	}

	public int getCount() {
		return mImageIds.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView i = new ImageView(mContext);
		i.setImageResource(mImageIds[position]);
		i.setLayoutParams(new Gallery.LayoutParams(150, 100));
		i.setScaleType(ImageView.ScaleType.FIT_XY);
		i.setBackgroundResource(mGalleryItemBackground);
		return i;
	}
}

class MyOnItemSelectedListener implements OnItemSelectedListener {

	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		Toast.makeText(parent.getContext(),
				"The planet is " + parent.getItemAtPosition(pos).toString(),
				Toast.LENGTH_LONG).show();
	}

	public void onNothingSelected(AdapterView parent) {
		// Do nothing.
	}
}